var imgSrc = []; //图片路径
var imgFile = []; //文件流
var imgName = []; //图片名字

var imgSrc2 = []; //图片路径
var imgFile2 = []; //文件流
var imgName2 = []; //图片名字

//选择图片
function imgUpload(obj,index) {
	var oInput = '#' + obj.inputId;
	var imgBox = '#' + obj.imgBox;
	//alert(imgBox); 
	var btn = '#' + obj.buttonId;
	$(oInput).on("change", function() {
		var fileImg = $(oInput)[0];
		var fileList = fileImg.files;
		console.log(fileList);
		if(index === 1){
			for(let i = 0; i < fileList.length; i++) {
				let imgSrcI = getObjectURL(fileList[i]);
				imgName.push(fileList[i].name);
				imgSrc.push(imgSrcI);
				console.log(imgSrc);
				imgFile.push(fileList[i]);
			}
			addNewContent(imgBox,index);
		}else if(index === 2){
			for(let i = 0; i < fileList.length; i++) {
				let imgSrcI = getObjectURL(fileList[i]);
				imgName2.push(fileList[i].name);
				imgSrc2.push(imgSrcI);
				imgFile2.push(fileList[i]);
				console.log(imgSrc2);
			}
			addNewContent(imgBox,index);
		}
		fileList = null;
	})

	$(btn).on('click', function() {
		if(!limitNum(obj.num)){
		  	alert("超过限制");
		  	return false;
		}
		//用formData对象上传
		var fd = new FormData($('#upBox')[0]);
		if(index === 1){
			for(var i=0;i<imgFile.length;i++){
				fd.append(obj.data+"[]",imgFile[i]);
			}
		}else if (index === 2){
			for(var i=0;i<imgFile2.length;i++){
				fd.append(obj.data+"[]",imgFile2[i]);

			}
		}
		submitPicture(obj.upUrl, fd);
	})
}
//图片展示
function addNewContent(obj,index) {
	$(obj).html("");
	if(index === 1){
		for(var a = 0; a < imgSrc.length; a++) {
			var oldBox = $(obj).html();
			$(obj).html(oldBox + '<div class="imgContainer"><img title=' + imgName[a] + ' alt=' + imgName[a] + ' src=' + imgSrc[a] +
			 ' onclick="removeImg(this,' + a + ',1)"></div>');
		}
	}else if(index === 2){
		for(var a = 0; a < imgSrc2.length; a++) {
			var oldBox = $(obj).html();
			$(obj).html(oldBox + '<div class="imgContainer"><img title=' + imgName2[a] + ' alt=' + imgName2[a] + ' src=' + imgSrc2[a] +
			 ' onclick="removeImg(this,' + a + ',2)"></div>');
		}
	}
	
}
//删除
function removeImg(obj, index,ind) {
/*
bug:1.删除过的无法再次选择.
	2.第二次选择不同的图片时，会出现两张图片.
	3.第二次及以后若选择删除过的这张，下次选不同的时候也会把这个次数算上.
	例如 第一张删除之后，选第二张会出现两张，而第二次选删除过的那张，
	第三次再选则第一张图片出现一次，第二张图片会出现三次
*/
	/*if(ind === 1 ){
		imgSrc.splice(index, 1);  
		imgFile.splice(index, 1);
		imgName.splice(index, 1);
	}else if(ind === 2 ){
		imgSrc2.splice(index, 1);  
		imgFile2.splice(index, 1);
		imgName2.splice(index, 1);

	}*/	
	if(ind === 1){
		imgSrc = [];
		imgFile = [];
		imgName = [];
	}
	console.log(imgSrc);
	console.log(imgFile);
	console.log(imgName);

	/*var boxId = "#" + $(obj).parent('.imgContainer').parent().attr("id");
	addNewContent(boxId,ind);*/
$(obj).parent().remove();

}
//限制图片个数
function limitNum(num){
	if(!num){
		return true;
	}else if(imgFile.length>num){
		return false;
	}else if(imgFile2.length>num){
		return false;
	}else{
		return true;
	}
}

//上传(将文件流数组传到后台)
function submitPicture(url,data) {
    for (var p of data) {
	  	console.log(p);
	}
	if(url&&data){
		$.ajax({
			type: "post",
			url: url,
			async: true,
			data: data,
			processData: false,
			contentType: false,
			success: function(dat) {
				console.log(dat);
			}
		});
	}else{
	  alert('请打开控制台查看传递参数！');
	}
}
//图片灯箱
function imgDisplay(obj) {
	var src = $(obj).attr("src");
	var imgHtml = '<div style="width: 100%;height: 100vh;overflow: auto;background: rgba(0,0,0,0.5);text-align: center;position: fixed;top: 0;left: 0;z-index: 1000;"><img src=' + src + ' style="margin-top: 100px;width: 70%;margin-bottom: 100px;"/><p style="font-size: 50px;position: fixed;top: 30px;right: 30px;color: white;cursor: pointer;" onclick="closePicture(this)">×</p></div>'
	$('body').append(imgHtml);
}
//关闭
function closePicture(obj) {
	$(obj).parent("div").remove();
}

//图片预览路径
function getObjectURL(file) {
	var url = null;
	if(window.createObjectURL != undefined) { // basic
		url = window.createObjectURL(file);
	} else if(window.URL != undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file);
	} else if(window.webkitURL != undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}